package com.example.todolist;

import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteBindOrColumnIndexOutOfRangeException;
import android.database.sqlite.SQLiteDatabase;

import java.util.ArrayList;
import java.util.List;

public class DBManager {

    private DBHelper dbHelper;
    private String TBNAME;

    public DBManager(Context context) {
        dbHelper = new DBHelper(context);
        TBNAME = DBHelper.TB_NAME;
    }

    public void add(ListItem item){
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put("title", item.getTitle());
        values.put("detail", item.getDetail());
        values.put("date", item.getDate());
        values.put("situation", "未完成");
        values.put("ddl", item.getDdl());
        db.insert(TBNAME, null, values);
        db.close();
    }


    public void delete(int id){
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        db.delete(TBNAME, "_id=?", new String[]{String.valueOf(id)});
        db.close();
    }

    public void update(ListItem item){
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put("title", item.getTitle());
        values.put("detail", item.getDetail());
        values.put("date", item.getDate());
        values.put("situation", item.getSituation());
        values.put("ddl", item.getDdl());
        db.update(TBNAME, values, "_id=?", new String[]{String.valueOf(item.getId())});
        db.close();
    }

    @SuppressLint("Range")
    public List<ListItem> listAll(){
        List<ListItem> List = null;
        SQLiteDatabase db = dbHelper.getReadableDatabase();
        Cursor cursor = db.query(TBNAME, null, null, null, null, null, "ddl asc");
        if(cursor!=null){
            List = new ArrayList<ListItem>();
            while(cursor.moveToNext()){
                ListItem item = new ListItem();
                item.setId(cursor.getInt(cursor.getColumnIndex("_id")));
                item.setTitle(cursor.getString(cursor.getColumnIndex("title")));
                item.setDetail(cursor.getString(cursor.getColumnIndex("detail")));
                item.setDate(cursor.getString(cursor.getColumnIndex("date")));
                item.setSituation(cursor.getString(cursor.getColumnIndex("situation")));
                item.setDdl(cursor.getString(cursor.getColumnIndex("ddl")));

                List.add(item);
            }
            cursor.close();
        }
        db.close();
        return List;

    }

    @SuppressLint("Range")
    public ListItem findById(int id){
        SQLiteDatabase db = dbHelper.getReadableDatabase();
        Cursor cursor = db.query(TBNAME, null, "_id=?", new String[]{String.valueOf(id)}, null, null, null);
        ListItem ListItem = null;
        if(cursor!=null && cursor.moveToFirst()){
            ListItem = new ListItem();
            ListItem.setId(cursor.getInt(cursor.getColumnIndex("_id")));
            ListItem.setTitle(cursor.getString(cursor.getColumnIndex("title")));
            ListItem.setDetail(cursor.getString(cursor.getColumnIndex("detail")));
            ListItem.setDate(cursor.getString(cursor.getColumnIndex("date")));
            ListItem.setSituation(cursor.getString(cursor.getColumnIndex("situation")));
            ListItem.setDdl(cursor.getString(cursor.getColumnIndex("ddl")));
            cursor.close();
        }
        db.close();
        return ListItem;
    }

}
